package com.ccc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import com.ccc.dao.UserDao;
import com.ccc.model.User;
import com.ccc.util.dao.UtilDao;

@Component("userDao")
public class UserDaoImpl implements UserDao {
	
	private UtilDao utilDao;
	
	@Resource
	private SessionFactory sessionFactory;
	
	public UtilDao getUtilDao() {
		return utilDao;
	}
    
	@Resource
	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

	@Override
	public void addUser(User user) {
		utilDao.save(user);

	}

	@Override
	public void deleteUser(User user) {
		utilDao.delete(user);

	}

	@Override
	public void updateUser(User user) {
		utilDao.update(user);

	}

	@Override
	public User findUser(int id) {
		return utilDao.find(User.class, id);
	}

	@Override
	public List<User> findUser(String sql, Object[] o) {
		return utilDao.find(sql, o);
	}

	@Override
	public User findFirstUser() {
		String sql = "from User user where user.userId=(select min(userId) from User)";
		return (User)this.sessionFactory.getCurrentSession().createQuery(sql).uniqueResult();
	}

}
